using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

namespace QFramework.Framework.ResKit.HotUpdate
{
    /// <summary>
    ///     热更新配置类
    /// </summary>
    public class HotUpdateConfig
    {
        /// <summary>
        ///     热更 AssetBundles 资源存放路径
        /// </summary>
        public virtual string HotUpdateAssetBundlesFolder => Application.persistentDataPath + "/AssetBundles/";

        /// <summary>
        ///     本地 资源信息文件 路径
        /// </summary>
        public virtual string LocalResVersionFilePath => Application.streamingAssetsPath + "/AssetBundles/" +
                                                         ResKitUtil.GetPlatformName() + "/ResVersion.json";

        /// <summary>
        ///     本地 AssetBundles 资源存放路径
        /// </summary>
        public virtual string LocalAssetBundlesFolder => Application.streamingAssetsPath + "/AssetBundles/" +
                                                         ResKitUtil.GetPlatformName() + "/";

        /// <summary>
        ///     远程 资源版本配置文件 URL路径
        /// </summary>
        public virtual string RemoteResVersionURL =>
            Application.dataPath + "/QFramework/Framework/ResKit/HotUpdate/Remote/ResVersion.json";

        /// <summary>
        ///     远程 AssetBundles 资源 URL 基础路径
        /// </summary>
        public virtual string RemoteAssetBundlesURLBase =>
            Application.dataPath + "/QFramework/Framework/ResKit/HotUpdate/Remote/";


        /// <summary>
        ///     加载热更新 AssetBundles 资源版本
        /// </summary>
        /// <returns></returns>
        public virtual ResVersion LoadHotUpdateAssetBundlesFolderResVersion()
        {
            var persistentResVersionFilePath = HotUpdateAssetBundlesFolder + "ResVersion.json";

            // 文件不存在，则直接返回 null
            if (!File.Exists(persistentResVersionFilePath)) return null;

            var persistentResVersionJson = File.ReadAllText(persistentResVersionFilePath);
            var persistentResVersion = JsonUtility.FromJson<ResVersion>(persistentResVersionJson);

            return persistentResVersion;
        }

        /// <summary>
        ///     获取 StreamingAsset 目录下资源版本
        /// </summary>
        /// <param name="getResVersion"></param>
        /// <returns></returns>
        public virtual IEnumerator GetStreamingAssetResVersion(Action<ResVersion> getResVersion)
        {
            var streamingResVersionFilePath = LocalResVersionFilePath;

            var www = new UnityWebRequest(streamingResVersionFilePath);
            www.downloadHandler = new DownloadHandlerBuffer();

            www.SendWebRequest();
            yield return null;

            var streamingResVersion = JsonUtility.FromJson<ResVersion>(www.downloadHandler.text);
            getResVersion(streamingResVersion);
        }

        /// <summary>
        ///     请求 远程服务器 资源版本
        /// </summary>
        /// <param name="onResDownLoaded"></param>
        /// <returns></returns>
        public virtual IEnumerator RequestRemoteResVersion(Action<ResVersion> onResDownLoaded)
        {
            var remoteResVersionPath = RemoteResVersionURL;

            // unity web请求
            var www = new UnityWebRequest(remoteResVersionPath);

            // 保存对DownloadHandler对象的引用，该对象管理UnityWebRequest从远程服务器接收的主体数据。
            www.downloadHandler = new DownloadHandlerBuffer();

            // 开始与远程服务器通信
            www.SendWebRequest();
            yield return null;

            var jsonString = www.downloadHandler.text;
            var resVersion = JsonUtility.FromJson<ResVersion>(jsonString);

            onResDownLoaded(resVersion);
        }
    }
}